package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.SysSourceRegistry;
import com.ruoyi.system.domain.dto.QueryRegistryDto;
import com.ruoyi.system.mapper.SysSourceRegistryMapper;
import com.ruoyi.system.service.ISysSourceRegistryService;
import com.ruoyi.system.service.ToolKitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 外部资源工具注册Service业务层处理
 * 
 * @author metaee
 * @date 2023-12-14
 */
@Service
@Slf4j
public class SysSourceRegistryServiceImpl extends ServiceImpl<SysSourceRegistryMapper,SysSourceRegistry> implements ISysSourceRegistryService
{
    @Autowired
    private SysSourceRegistryMapper sysSourceRegistryMapper;

    @Autowired
    private ToolKitService toolKitService;

    /**
     * 查询外部资源工具注册
     * 
     * @param id 外部资源工具注册主键
     * @return 外部资源工具注册
     */
    @Override
    public SysSourceRegistry selectSysSourceRegistryById(Long id)
    {
        return sysSourceRegistryMapper.selectSysSourceRegistryById(id);
    }

    /**
     * 根据名称查询外部资源工具注册
     *
     * @param resourceName 资源名称
     * @return 外部资源工具注册
     */
    @Override
    public SysSourceRegistry selectExistSysSourceRegistryByName(String resourceName) {
        return sysSourceRegistryMapper.selectExistSysSourceRegistryByName(resourceName, 0);
    }

    /**
     * 查询外部资源工具注册列表
     * 
     * @param queryRegistryDto 分页
     * @return 外部资源工具注册
     */
    @Override
    public Page<SysSourceRegistry> selectSysSourceRegistryPage(QueryRegistryDto queryRegistryDto)
    {
        Page page = new Page<>(queryRegistryDto.getPage(), queryRegistryDto.getSize());
        IPage<SysSourceRegistry> result = sysSourceRegistryMapper.selectSysSourceRegistryPage(page, queryRegistryDto.getName(), queryRegistryDto.getTools(), 0);
        return (Page<SysSourceRegistry>) result;
    }

    /**
     * 新增外部资源工具注册
     * 
     * @param sysSourceRegistry 外部资源工具注册
     * @return 结果
     */
    @Override
    public int insertSysSourceRegistry(SysSourceRegistry sysSourceRegistry)
    {
        return sysSourceRegistryMapper.insertSysSourceRegistry(sysSourceRegistry);
    }

    /**
     * 修改外部资源工具注册
     * 
     * @param sysSourceRegistry 外部资源工具注册
     * @return 结果
     */
    @Override
    public int updateSysSourceRegistry(SysSourceRegistry sysSourceRegistry)
    {
        return sysSourceRegistryMapper.updateSysSourceRegistry(sysSourceRegistry);
    }

    /**
     * 批量删除外部资源工具注册
     * 
     * @param ids 需要删除的外部资源工具注册主键
     * @return 结果
     */
    @Override
    public int deleteSysSourceRegistryByIds(Long[] ids)
    {
        return sysSourceRegistryMapper.deleteSysSourceRegistryByIds(ids);
    }

    /**
     * 删除外部资源工具注册信息
     * 
     * @param id 外部资源工具注册主键
     * @return 结果
     */
    @Override
    public int deleteSysSourceRegistryById(Long id)
    {
        return sysSourceRegistryMapper.deleteSysSourceRegistryById(id);
    }


    @Override
    public List<SysSourceRegistry> getToolByTkName(String tkName) {
        LambdaQueryWrapper<SysSourceRegistry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysSourceRegistry::getTools,tkName).eq(SysSourceRegistry::getIsDeleted,0);
        List<SysSourceRegistry> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public String getPromptByTName(String tName) {
        LambdaQueryWrapper<SysSourceRegistry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysSourceRegistry::getResourceName,tName).eq(SysSourceRegistry::getIsDeleted,0);
        List<SysSourceRegistry> list = this.list(queryWrapper);
        if (list == null || list.size() == 0){
            throw new ServiceException("该工具不存在！");
        }
        return list.get(0).getToolPrompt();
    }

    @Override
    public void updateToolKitPrompt(String toolKitName , String toolKitUpdatePrompt){
        boolean isDone = toolKitService.updateToolKitPromptByTkName(toolKitName, toolKitUpdatePrompt);
        if (isDone) {
            log.info("ToolKit Prompt 动态拼接成功！");
        }else {
            log.info("ToolKit Prompt 动态拼接失败！");
        }
    }
}
